// DarkSpace.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "DarkSpace.h"
#include "DarkSpaceDlg.h"

#ifdef _DEBUG
//#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//---------------------------------------------------------------------------------------------------

static CString GetKeyValue( const TCHAR * pKey, const TCHAR * pDefault )
{
	TCHAR sValue[ 1024 ];
	sValue[ 0 ] = 0;

	GetPrivateProfileString( _T("DarkSpace"), pKey, pDefault != NULL ? pDefault : _T(""), sValue, 1024, _T(".\\config.ini") );
	return CString( sValue );
}

static HANDLE StartProcess( const TCHAR * pCommandLine )
{
    STARTUPINFO			si;
    PROCESS_INFORMATION pi;

	memset( &si, 0, sizeof(si) );
    si.cb = sizeof(si);

	char sCommandLine[ MAX_PATH ];
	strcpy( sCommandLine, pCommandLine );

	if ( !CreateProcess( NULL,					// No module name (use command line). 
        sCommandLine,							// Command line. 
        NULL,									// Process handle not inheritable. 
        NULL,									// Thread handle not inheritable. 
        FALSE,									// Set handle inheritance to FALSE. 
        CREATE_NEW_PROCESS_GROUP,				// Creation flags. 
        NULL,									// Use parent's environment block. 
        NULL,									// Use parent's starting directory. 
        &si,									// Pointer to STARTUPINFO structure.
        &pi ))									// Pointer to PROCESS_INFORMATION structure.
	{
		// failed
		return NULL;
	}

	return( pi.hProcess );
}

/////////////////////////////////////////////////////////////////////////////
// CDarkSpaceApp

BEGIN_MESSAGE_MAP(CDarkSpaceApp, CWinApp)
	//{{AFX_MSG_MAP(CDarkSpaceApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDarkSpaceApp construction

CDarkSpaceApp::CDarkSpaceApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDarkSpaceApp object

CDarkSpaceApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDarkSpaceApp initialization

#pragma warning(disable:4996)	// warning C4996: 'CWinApp::Enable3dControls' was declared deprecated

BOOL CDarkSpaceApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	//if ( StartProcess( GetKeyValue( "OnLobby", "GCQL.exe -g 1" ) ) == NULL )
	//	MessageBox( NULL, _T("Failed to start GameCQ, please reinstall!"), "Error", MB_OK );
	// set the background color
	SetDialogBkColor( RGB(0, 0, 0), RGB(0, 0, 0) );
	// show the dialog
	CDarkSpaceDlg().DoModal();

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return TRUE;
}
